﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Drawing;
using System.Windows.Threading;
using System.Threading;

namespace PedesTrainProject.Grid
{
    [Serializable()]
    public class CarLight : Light, ISerializable
    {
        private bool yellow=false;
        
        private bool previousGreen=false;

        public bool Yellow
        {
            get { return yellow; }
            set { yellow = value; }
        }

        public bool Green
        {
            get { return green; }
            set { green = value; }
        }

        public bool Red
        {
            get { return red; }
            set { red = value; }
        }

        private Directions direction;

        public Directions Direction
        {
            get { return direction; }
            set { direction = value; }
        }

       // public CarLight(int[] intervals)


        public CarLight()
        {

        }

        public CarLight(List<int> _intervals, Directions _direction) : base(_intervals)
        {
            intervals = _intervals;
            direction = _direction;
            Green = false;
            Red = false;
            yellow = false;

            if (_direction == Directions.North || _direction == Directions.South)
            {
                green = true;
            }
            else
                red = true;
        }

        public void change(Core.ICoreGUIRun coreGUIRun,int pos)
        {
            
            while (true)
            {
                if (Green == true)
                {
                    coreGUIRun.changeLights(Convert.ToInt32(Core.Calculations.gridPosition(pos).X * 100), Convert.ToInt32(Core.Calculations.gridPosition(pos).Y * 100), Color.Green, direction);
                    coreGUIRun.switchLanesDirection(pos);
                    Thread.Sleep(intervals[0]/coreGUIRun.getSpeed());
                    yellow = true;
                    previousGreen = true;
                    Green = false;
                    Red = false;
                    
                }
                if (Red == true)
                {
                    coreGUIRun.changeLights(Convert.ToInt32(Core.Calculations.gridPosition(pos).X * 100), Convert.ToInt32(Core.Calculations.gridPosition(pos).Y * 100), Color.Red, direction);
                    coreGUIRun.switchLanesDirection(pos);
                    Thread.Sleep(intervals[1] / coreGUIRun.getSpeed());
                    yellow = true;
                    Green = false;
                    Red = false;
                    previousGreen = false;
                    
                }
                if (yellow == true)
                {
                    coreGUIRun.changeLights((int)Core.Calculations.gridPosition(pos).X * 100, (int)Core.Calculations.gridPosition(pos).Y * 100, Color.Yellow, direction);
                    Thread.Sleep(intervals[2] / coreGUIRun.getSpeed());
                    Red = false;
                    Green = false;
                    if (previousGreen) Red = true;
                    else Green = true;
                    yellow = false;
                    
                }
            }
        }

        #region Serialize
        
        public CarLight(SerializationInfo info, StreamingContext ctxt)
        {
            this.yellow = (bool)info.GetValue("Yellow", typeof(bool));
            this.Red = (bool)info.GetValue("Red", typeof(bool));
            this.Green = (bool)info.GetValue("Green", typeof(bool));
            this.Intervals = (List<int>)info.GetValue("Intervals", typeof(List<int>));
            this.name = (string)info.GetValue("Name", typeof(string));
            this.skin = (string)info.GetValue("Skin", typeof(string));
            this.previousGreen = (bool)info.GetValue("PreviousGreen", typeof(bool));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("Yellow", this.yellow);
            info.AddValue("Red", this.Red);
            info.AddValue("Green", this.Green);
            info.AddValue("Intervals", this.Intervals);
            info.AddValue("Name", this.name);
            info.AddValue("Skin", this.skin);
            info.AddValue("PreviousGreen", this.previousGreen);
        }
        
        #endregion
    }
}
